package CON;
use strict;
use warnings;
use Term::ANSIColor;


use Exporter qw/import/;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(color_or_not *CON *CoN CoN_modulo *CONm);

my @AColors_fg = ( "underline  green", "underline  yellow", "underline bold  blue", "underline  magenta", "underline  cyan", "underline  red", );
push @AColors_fg, ( "bold green", "bold yellow", "bold blue", "bold magenta", "bold cyan", "bold red", );

sub color_or_not {
    my ( $str, $c1, $c2 ) = @_;
    if ( not $c1 ) { $c1 = q/bold blue/; }
    if ( not $c2 ) { $c2 = q/on_black/; }
    if ( -t STDOUT ) { return colored( $str, $c1, $c2 ); }
    else             { return $str; }
}

sub CoN_modulo {
    my ($pat, $it) = @_;
    $it = $it // 1;
    my $it2= (int $it) % ($#AColors_fg+1);
    return &color_or_not( $pat, $AColors_fg[$it2]);
}

# sub-routine aliasing
*CON  = \&color_or_not;
*CoN  = \&color_or_not;
*CONm = \&CoN_modulo;

1;

__END__

=head1 NAME


CON: Color_Or_Not - to detect output terminal; if STDOUT, highlight  them (default is onto black background)

=head1 SYNOPSIS

    use CON;

    # to show font in "underline yellow"
    printf "-i- showing filename %s\n", &CONm("file1.txt", 1);

    # a lower level of tweaking; e.g.,  to show font in "bold blue"
    printf "-i- showing filename %s\n", &CON("file2.txt", "bold blue");

=head1 DESCRIPTION

This module provides an easier usage over detecting output
terminal/pipe; it plainly uses perl's C<-t> operator.
If C<-t> returns true, this module returns string with
C<colored()> sub-routine inside C<Term::ANSIColor> module.

I<CON> stands for I<Color or Not>; and I<CONm> is to take in an
integer number controlling the output font color (I<m> stands
for modulo, since we are re-using the limited number of colors).

=head1 FUNCTIONS

L<CON> module implements the following functions.

=head2  CON(), CoN()

    use CON;
    my $str  = &CON( "message1 ", "bold red" );
    my $str2 = &CON( "message2 ", "bold green" );
    printf "-i-we got message %8s and %8s\n", $str, $str2;

Please see L<Term::ANSIColor> for description of font color.
Terminal varies. E.g., color "red" is too dark for the
author's terminal, thus the author only uses "bold red".

=head2  CONm()

    use CON qw(*CONm);
    my $str3 = &CONm( "message3 ", 2 );
    my $str4 = &CONm( "message4 ", 4 );
    printf "-i-we also got message %8s and %8s\n", $str3, $str4;

    my $str5 = q//;
    foreach my $i0 ( 0 .. 25 ) {
        $str5 .= &CONm( "message-$i0; ", $i0 );
    }

The list of font colors are defined like below;
any number greater than the list will be
modulo'ed with list's size (in this case, 12).

=over 

=item 0 : "underline  green"

=item 1 : "underline  yellow"

=item 2 : "underline bold  blue"

=item 3 : "underline  magenta"

=item 4 : "underline  cyan"

=item 5 : "underline  red"

=item 6 : "bold green"

=item 7 : "bold yellow"

=item 8 : "bold blue"

=item 9 : "bold magenta"

=item 10: "bold cyan"

=item 11: "bold red"

=back




=head1 See Also

L<Term::ANSIColor>
